package com.kb.erp.mapper.sqlServer.crm;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.erp.domain.CrmCustomerDept;
import com.kb.erp.entity.crm.Customer;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CRMCustomerMapper extends BaseMapper<Customer> {
    @Select("<script>" +
            "SELECT " +
            "    Customer.CustCode, " +
            "    Customer.CustName, " +
            "    CRM_Department.name AS DepartmentName, " +
            "    CRM_Department.code AS DepartmentCode, " +
            "    Xnh_UserInfo.username " +
            "FROM [dbo].[Customer] " +
            "LEFT JOIN CRM_Department ON Customer.departmentId = CRM_Department.recId " +
            "LEFT JOIN Xnh_UserInfo ON Xnh_UserInfo.rkey = Customer.sysuserId " +
            "<where>" +
            "    <if test='customerDept.salesmanList != null and customerDept.salesmanList.size() > 0'>" +
            "        AND Customer.sysuserId IN " +
            "        <foreach item='rkey' index='index' collection='customerDept.salesmanList' open='(' separator=',' close=')'>" +
            "            #{rkey}" +
            "        </foreach>" +
            "    </if>" +
            "    <if test='customerDept.dept != null and customerDept.dept != &quot;&quot;'> " +
            "        AND  CRM_Department.recId = #{customerDept.dept}" +
            "    </if>" +
            "</where>" +
            "</script>")
    public Page<CrmCustomerDept> queryCrmCustomerDept(Page<CrmCustomerDept> page ,CrmCustomerDept customerDept);

    //Customer表有约束 [customer_edit] 触发器，当制作中的客户并且;流程客户修改时 不能直接修改客户数据 可以暂时去掉约束
//    @Select("UPDATE [dbo].[Customer]  SET departmentId = #{dept} WHERE status != 'Active' and sysuserId = (#{salesman})")
    @Select("UPDATE [dbo].[Customer]  SET departmentId = #{dept} WHERE sysuserId = (#{salesman})")
    public void updateCustomerDept(@Param("salesman") String salesman,@Param("dept") String dept);

    @Select("UPDATE [dbo].[CompanyUserRelation]  SET UserId = #{UserId} WHERE CompanyId = (#{CompanyId})")
    public void updateCompanyUserRelation(@Param("UserId") Integer UserId,@Param("CompanyId") Integer CompanyId);
    //UPDATE [dbo].[CRM_CashReceipt] SET [note] = ' 1' WHERE [recId] = 1975548
    @Select("UPDATE [dbo].[CRM_CashReceipt] SET [status] = 'Confirmed' WHERE [recId] = #{id}")
    public void batchSubmit(@Param("id") String id);
}

